<template>
  <div>
    <div v-if="isShow">
      <!-- 条件查询上传订单表单 -->
      <div class="check-form-box">
        <el-form
          :inline="true"
          :model="searchModel.data"
          class="demo-form-inline"
        >
          <el-form-item label="订单日期">
            <el-date-picker
              v-model="searchModel.createDate"
              type="daterange"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
            />
          </el-form-item>
          <el-form-item label="车牌号">
            <el-input
              v-model.trim="searchModel.carNo"
              placeholder="请输入车牌号"
            />
          </el-form-item>
          <el-form-item label="手机号码">
            <el-input
              v-model.trim="searchModel.mobile"
              :formatter="(value) => value.replace(/[^\d]/g, '')"
              placeholder="请输入手机号码"
            />
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="searchModel.state" placeholder="请选择">
              <el-option label="全部" :value="null" />
              <el-option label="审核中" value="0" />
              <el-option label="审核通过" value="1" />
              <el-option label="审核未通过" value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="审核日期">
            <el-date-picker
              v-model="searchModel.createCheckDate"
              type="daterange"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
            />
          </el-form-item>
          <el-form-item label="系列名称">
            <el-input
              v-model.trim="searchModel.packSeriesName"
              placeholder="请输入系列名称"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" color="#ea5b24" @click="searchOrder()"
              >查询</el-button
            >
            <el-button
              type="primary"
              color="#ea5b24"
              @click="exportOrder()"
              v-if="pinia.isShow[109]"
              >导出Excel</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <!-- 汇总数据 -->
      <div class="div-summaryData">
        <span class="text-summaryData"
          >用户上报金额：{{
            summaryData.totalUserAmt
              ? summaryData.totalUserAmt.toFixed(2)
              : "0.00"
          }}</span
        >
        <span class="text-summaryData"
          >审核金额：{{
            summaryData.totalCostAmt
              ? summaryData.totalCostAmt.toFixed(2)
              : "0.00"
          }}</span
        >
        <span class="text-summaryData"
          >实际奖励金额：{{
            summaryData.totalAwardAmt
              ? summaryData.totalAwardAmt.toFixed(2)
              : "0.00"
          }}</span
        >
      </div>
      <!-- 上传订单列表 -->
      <div>
        <el-table :data="tableData.data" style="width: 100%">
          <el-table-column prop="orderDate" label="订单日期" width="160" />
          <el-table-column prop="packSeriesName" width="100" label="系列名称" />
          <el-table-column prop="userId" label="用户ID" />
          <el-table-column prop="userName" width="110" label="车主姓名" />
          <el-table-column prop="userMobile" width="110" label="用户手机号码" />
          <el-table-column prop="topAgentId" width="110" label="一级代理商ID" />
          <el-table-column
            prop="topAgentName"
            width="130"
            label="一级代理商姓名"
          />
          <el-table-column prop="carNo" label="车牌号" />
          <el-table-column prop="costAmt" label="审核金额" />
          <el-table-column prop="goldAmt" width="110" label="兑换金币数量" />
          <el-table-column prop="userAmt" width="110" label="用户上报金额" />
          <el-table-column width="100" label="状态">
            <template #default="scope">
              <div
                @click="showReason(scope.row)"
                :style="{ color: scope.row.state == 2 ? 'red' : 'black' }"
              >
                {{ scope.row.stateStr }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="checkerName" label="审核人" />
          <el-table-column prop="checkDate" label="审核时间" width="160" />
          <el-table-column label="操作" v-if="pinia.isShow[110]">
            <template v-slot:default="scope">
              <el-button
                link
                type="primary"
                size="small"
                @click="showDetails(scope.row)"
              >
                {{ "详情" }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页插件 -->
        <div style="text-align: center">
          <el-pagination
            :total="totalNum"
            @current-change="handleCurrentChange"
            background
            :page-size="size"
            layout="prev, pager, next, total"
            :current-page="page"
          />
        </div>
      </div>
      <!-- 审核不通过原因 -->
      <el-dialog title="审核失败原因" v-model="dialogVisible" width="30%">
        <span>{{ reason }}</span>
      </el-dialog>
    </div>
    <div v-else>
      <detaild :orderInfo="detaildData" @packConfigBack="showDetails"></detaild>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import {
  findUploadOrderApi,
  findUploadExcelApi,
  findTotalUploadOrderApi,
} from "../../../api/index";
import { formatDateTime } from "../../../utils/formatDateTime";
import detaild from "./detaild.vue";
import { usePermissStore } from "../../../store/permiss";
const pinia = usePermissStore();

// 分页列表数据
let tableData: any = reactive([]);
const totalNum = ref(0);
const page = ref(1);
const size = 8;

// 控制详情组件显示
const isShow = ref(true);
// 控制审核失败原因显示
const dialogVisible = ref(false);
// 审核不通过原因
const reason = ref("");

// 翻页
let flipData = reactive({
  pageNum: page,
  pageSize: size,
});
const handleCurrentChange = (val: number) => {
  page.value = val;
  flipData.pageNum = page.value;
  findOrder(flipData);
};

// 传入详情组件参数
const detaildData = ref({
  checkInfoId: null,
});

// 搜索模型
const searchModel: any = reactive({
  carNo: null,
  mobile: null,
  state: null,
  createDate: null,
  startOrderDate: null,
  endOrderDate: null,
  createCheckDate: null,
  startCheckDate: null,
  endCheckDate: null,
  packSeriesName: null,
  pageNum: page,
  pageSize: size,
});

// 汇总数据
const summaryData: any = reactive({
  totalAwardAmt: null, //实际奖励金额
  totalCostAmt: null, //审核金额
  totalGoldAmt: null, //金币
  totalUserAmt: null, //用户申请金额
});

// 搜索上传订单
const searchOrder = () => {
  page.value = 1;
  if (searchModel.createDate) {
    searchModel.startOrderDate = formatDateTime(searchModel.createDate[0]);
    searchModel.endOrderDate = formatDateTime(
      searchModel.createDate[1]
    ).replace("00:00:00", "23:59:59");
  } else {
    searchModel.startOrderDate = null;
    searchModel.endOrderDate = null;
  }
  if (searchModel.createCheckDate) {
    searchModel.startCheckDate = formatDateTime(searchModel.createCheckDate[0]);
    searchModel.endCheckDate = formatDateTime(
      searchModel.createCheckDate[1]
    ).replace("00:00:00", "23:59:59");
  } else {
    searchModel.startCheckDate = null;
    searchModel.endCheckDate = null;
  }
  searchParmSearch();
  findOrderTotal();
  findOrder(searchModel);
  flipData = JSON.parse(JSON.stringify(searchModel));
};

// 查询上传订单
const findOrder = (searchModel) => {
  findUploadOrderApi(searchModel).then((res) => {
    if (res.data.code === 2000) {
      tableData.data = res.data.data.items;
      totalNum.value = res.data.data.total;
    } else {
      ElMessage.error("查询失败");
    }
  });
};

// 查询上传订单统计数据
const findOrderTotal = () => {
  findTotalUploadOrderApi({
    carNo: searchModel.carNo,
    mobile: searchModel.mobile,
    state: searchModel.state,
    startOrderDate: searchModel.startOrderDate,
    endOrderDate: searchModel.endOrderDate,
    startCheckDate: searchModel.startCheckDate,
    endCheckDate: searchModel.endCheckDate,
    packSeriesName: searchModel.packSeriesName,
  }).then((res) => {
    if (res.data.code === 2000) {
      summaryData.totalAwardAmt = res.data.data.totalAwardAmt ?? 0;
      summaryData.totalCostAmt = res.data.data.totalCostAmt ?? 0;
      summaryData.totalGoldAmt = res.data.data.totalGoldAmt ?? 0;
      summaryData.totalUserAmt = res.data.data.totalUserAmt ?? 0;
    } else {
      ElMessage.error("统计数据查询失败");
    }
  });
};

// 搜索参数检查
const searchParmSearch = () => {
  Object.keys(searchModel).forEach((key) => {
    if (!searchModel[key]) {
      searchModel[key] = null;
    }
  });
};

// 展示审核不通过原因
const showReason = (item) => {
  if (item.state === 2) {
    reason.value = item.checkerNote;
    dialogVisible.value = true;
  }
};

// 展示详情
const showDetails = (item = null) => {
  isShow.value = !isShow.value;
  findOrder(searchModel);
  if (item) {
    detaildData.value.checkInfoId = item.id;
  }
};

//导出订单
const exportOrder = () => {
  findUploadExcelApi(searchModel).then((res) => {
    const blob = new Blob([res.data]);
    const fileName = "上传订单数据.xls";
    const elink = document.createElement("a"); // 创建a标签
    elink.download = fileName; // 为a标签添加download属性
    elink.style.display = "none";
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click(); // 点击下载
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink); // 释放标签
  });
};

onMounted(() => {
  findOrderTotal();
  findOrder(searchModel);
});
</script>
<style scoped>
:deep(.demo-form-inline) {
  display: grid;
  justify-items: stretch;
  grid-template-columns: repeat(auto-fill, 25%);
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-form-item) {
  width: 90%;
}

.div-summaryData {
  background-color: rgb(187, 190, 195);
  height: 34px;
  margin-top: 20px;
  line-height: 33px;
}

.text-summaryData {
  color: black;
  font-size: 14px;
  margin-right: 30px;
  margin-left: 10px;
}
</style>
